<template>
    <div>
        <!--搜索框-->
        <div class="search-form">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="用户名">
                    <el-input v-model="searchForm.username" placeholder="请输入昵称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!--头部工具栏-->
        <div class="header-toolbox">
            <el-button type="danger" size="small" @click="batchDelete">批量删除</el-button>
        </div>

        <!--表格数据-->
        <div class="data-table">
            <el-table
                    :data="tableData"
                    @selection-change="handleSelectionChange"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55px">
                </el-table-column>
                <el-table-column
                    label="ID"
                    v-show="false">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="用户名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.username }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="IP">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.ip }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="参数"
                        :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.params }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="URI">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.uri }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="描述">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.description }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作时间">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="模块">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.model }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150px">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="foot-page">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchForm.currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="searchForm.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    class="foot-pageInfo">
            </el-pagination>
        </div>


    </div>

</template>

<script>
import {
    batchDeleteIdsAPI,
    batchDeleteUserAPI, getCurrentPageInfoAPI,
    getFullUserInfoAPI,
} from '@/request/api'

export default {
    name: "LogView",
    data() {
        return {
            total: 100,
            searchForm: {
                currentPage: 1,
                pageSize: 5,
            },
            tableData: [],
            multipleSelection: [],
        }
    },
    methods: {
        searchSubmit() {
            this.searchForm.pageSize = 5
            this.searchForm.currentPage = 1
            this.list()
            this.searchForm = {
                pageSize: this.searchForm.pageSize,
                currentPage:  this.searchForm.currentPage
            }
        },
        batchDelete() {
            this.$confirm('确认执行删除操作么, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                batchDeleteIdsAPI(this.multipleSelection).then(response => {
                    if (response.code == 200) {
                        this.$message.success({
                            type: 'success',
                            message: response.msg,
                            onClose: () => {
                                this.searchForm = {
                                    currentPage: 1,
                                    pageSize: 5,
                                }
                                this.list()
                            },
                        })
                        return
                    }
                    this.$message.error(response.msg)
                }).catch(error => {
                    this.$message.error('删除操作错误')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 用户删除
        handleDelete(index, row) {
            this.$confirm('确认执行删除操作么, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.multipleSelection = [row.id]
                batchDeleteIdsAPI(this.multipleSelection).then(response => {
                    if (response.code == 200) {
                        this.$message.success({
                            type: 'success',
                            message: response.msg,
                            onClose: () => {
                                this.searchForm = {
                                    currentPage: 1,
                                    pageSize: 5,
                                }
                                this.list()
                            },
                        })
                        return
                    }
                    this.$message.error(response.msg)
                }).catch(error => {
                    this.$message.error('删除操作错误')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 开启复选框
        handleSelectionChange(val) {
            this.multipleSelection = val.map(row => row.id)
        },
        // 页大小改变
        handleSizeChange(val) {
            this.searchForm.pageSize = val
            // this.$message.info(this.searchForm.pageSize)
            this.list()
        },
        // 当前页改变
        handleCurrentChange(val) {
            this.searchForm.currentPage = val
            // this.$message.info( this.searchForm.currentPage)
            this.list()
        },
        list() {
            getCurrentPageInfoAPI(this.searchForm).then(response => {
                // 进行判断 注入值
                if (response.code == 200) {
                    this.tableData = response.data.data;
                    this.total = response.data.count;
                    return;
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error("操作失败")
            })
        },
    },
    created() {
        this.list()
    }
}
</script>

<style scoped>
.demo-form-inline {
    float: left;
    padding: 15px 0px 5px 10px;

}

.header-toolbox {
    width: 180px;
    padding-bottom: 10px;
}

.foot-pageInfo {
    float: left;
}
</style>